package com.hui.controller;

import com.hui.entity.Course;
import com.hui.entity.CourseLesson;
import com.hui.entity.CourseSection;
import com.hui.entity.ResponseResult;
import com.hui.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/06/11:38
 * @Description:
 */
@RestController
@RequestMapping("/courseContent")
public class CourseContentController {
    @Autowired
    private CourseContentService courseContentService;
    // 根据课程ID查询章节与课时信息
    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLesson(Integer courseId){
        List<CourseSection> list = courseContentService.getCourseSectionWithLesson(courseId);
        return new ResponseResult(true,200,"查询成功",list);

    }

    // 回显章节对应的课程信息
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(Integer courseId){
        Course course=courseContentService.getCourseByCourseId(courseId);
        return new ResponseResult(true,200,"查询成功",course);
    }

    // 保存&修改章节信息
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection){
        if(courseSection.getId()==null){
            courseContentService.saveSection(courseSection);
            return new ResponseResult(true,200,"保存成功",null);
        }else{
            courseContentService.updateSection(courseSection);
            return new ResponseResult(true,200,"修改成功",null);
        }
    }

    // 修改章节状态
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(int id,int status){
        courseContentService.updateSectionStatus(id,status);
        Map<String,Integer> map = new HashMap<>();
        map.put("status",status);
        return  new ResponseResult(true,200,"修改成功",map);
    }

    // 保存课时信息
    @RequestMapping("/saveLesson")
    public ResponseResult saveLesson(@RequestBody CourseLesson courseLesson){
        courseContentService.saveLesson(courseLesson);
        return new ResponseResult(true,200,"保存成功",null);
    }
}
